<?php
/* explain:		
 * Project:     
 * File:        vedioUpload.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("utils/CommonUtils.php");
include_once("domain/UploadFileDomain.php");

class UploadFileImpl extends CommonUtils
{
	const TYPE_IMAGE	= 1;
	
	function __construct($DBTag)
	{
		parent::__construct();
		
		$this->DBTag 	= $DBTag;
	}

	public function getById($id) 
	{
		$sql = "SELECT width, height, extName, md5, fileSize FROM upload_files WHERE id = $id";
		
		$oUpf = null;
		$conn = null;
		
		try {
			$conn = $this->getConnection($this->DBTag);
			
			$rs = mysql_query($sql, $conn);
			if ($row = mysql_fetch_row($rs)) {
				$oUpf = new UploadFileDomain();

				$oUpf->setId($id);
				$oUpf->setWidth($row[0]);
				$oUpf->setHeight($row[1]);
				$oUpf->setExtName($row[2]);
				$oUpf->setMd5($row[3]);
				$oUpf->setFileSize($row[4]);
				
				
			} else {
				$this->message = "get data failed by Id. sql : $sql <br>";
				$this->message .= "error: " . mysql_error($conn);
			}
		} catch (Exception $e) {
			$this->message = $e->getMessage();
		}
		
		if($conn != null)
			$this->freeConnection($this->DBTag, $conn);
			
		return $oUpf;
	}
	
	public function getByMd5($md5) 
	{
		$sql = "SELECT width, height, extName, id, fileSize FROM upload_files WHERE md5 = '$md5'";
		
		$oUpf = null;
		$conn = null;
		
		try {
			$conn = $this->getConnection($this->DBTag);
			
			$rs = mysql_query($sql, $conn);
			if ($row = mysql_fetch_row($rs)) {
				$oUpf = new UploadFileDomain();

				$oUpf->setMd5($md5);
				$oUpf->setWidth($row[0]);
				$oUpf->setHeight($row[1]);
				$oUpf->setExtName($row[2]);
				$oUpf->setId($row[3]);				
				$oUpf->setFileSize($row[4]);				
				
			} else {
				$this->message = "get data failed by Id. sql : $sql <br>";
				$this->message .= "error: " . mysql_error($conn);
			}
		} catch (Exception $e) {
			$this->message = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
			
		return $oUpf;
	}
	
	/* 
	 *
	 * @param  string 	name 
 	 * @return int    	true: insert ID
	 */
    public function add($extName, $md5, $width, $height, $fileSize)
	{
		$sql_data_array = array(
			'extName' 		=> $extName,
			'md5' 			=> $md5,
			'width' 		=> $width,
			'height' 		=> $height,
			'fileSize' 		=> $fileSize
		);
		
		return $this->insertArray($this->DBTag, 'upload_files', $sql_data_array, " ON DUPLICATE KEY UPDATE width=Values(width), height=Values(height)");
	}
	
	/* modify
	 *
	 * @param  string 	name 
 	 * @param  return 	boolean
	 */
	public function edit( $id, $type, $extName, $md5, $fileSize)
	{
		$sql = "UPDATE upload_files SET type='$type', extName='$extName', md5='$md5', fileSize='$fileSize' WHERE id = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($id)
	{
		$sql = "DELETE FROM upload_files WHERE id = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function isExistedByMd5($md5)
	{
		$sql = "SELECT id FROM upload_files WHERE md5 = '$md5'";
		
		return $this->isExisted($this->DBTag, $sql);
	}
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function lists($where, $order, $page, $discount, $url = '')
	{
		return $this->getPageRows($this->DBTag, '*', "upload_files $where", $order, $page, $discount, $url);	
	}

}
?>
